package com.example.mynews;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonElement;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends Activity {
    private RecyclerView list;
    private Handler handler;
    private NewsData newsData;
    private MyAdapter adapter;
    private TextView tvTop;
    private TextView tvShehui;
    private TextView tvYule;
    private TextView tvTiyu;
    private TextView tvShishang;
    private String topUrl="http://v.juhe.cn/toutiao/index?type=top&key=02136b4b694d5059eb06a411c67fe0a8";
    private String shehuiUrl="http://v.juhe.cn/toutiao/index?type=shehui&key=02136b4b694d5059eb06a411c67fe0a8";
    private String yuleUrl="http://v.juhe.cn/toutiao/index?type=yule&key=02136b4b694d5059eb06a411c67fe0a8";
    private String tiyuUrl="http://v.juhe.cn/toutiao/index?type=tiyu&key=02136b4b694d5059eb06a411c67fe0a8";
    private String shishangUrl="http://v.juhe.cn/toutiao/index?type=shishang&key=02136b4b694d5059eb06a411c67fe0a8";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        adapter=new MyAdapter(MainActivity.this,newsData);
        list.setAdapter(adapter);
        initHandler();
        initData(topUrl);

        tvTop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initData(topUrl);
                Toast.makeText(MainActivity.this,"头条",Toast.LENGTH_SHORT).show();
            }
        });
        tvShehui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initData(shehuiUrl);
                Toast.makeText(MainActivity.this,"社会",Toast.LENGTH_SHORT).show();
            }
        });
        tvYule.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initData(yuleUrl);
                Toast.makeText(MainActivity.this,"娱乐",Toast.LENGTH_SHORT).show();
            }
        });
        tvTiyu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initData(tiyuUrl);
                Toast.makeText(MainActivity.this,"体育",Toast.LENGTH_SHORT).show();
            }
        });
        tvShishang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initData(shishangUrl);
                Toast.makeText(MainActivity.this,"时尚",Toast.LENGTH_SHORT).show();
            }
        });


    }

    private void initView() {
        list=findViewById(R.id.ListView);
        tvTop=findViewById(R.id.tv_top);
        tvTop=findViewById(R.id.tv_top);
        tvShehui=findViewById(R.id.tv_shehui);
        tvShishang=findViewById(R.id.tv_shishang);
        tvTiyu=findViewById(R.id.tv_tiyu);
        tvYule=findViewById(R.id.tv_yule);
    }

    private void initHandler() {
        handler=new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                if (message.what==2001){
                    Log.e("Main","handler success");
                    LinearLayoutManager llm = new LinearLayoutManager(MainActivity.this);
                    llm.setOrientation(LinearLayoutManager.VERTICAL);
                    list.setLayoutManager(llm);
                    String json= (String) message.obj;
                    newsData=new Gson().fromJson(json,NewsData.class);
                    adapter.dataChanged(newsData);
                    return true;
                }
                return false;
            }
        });
    }



    private void initData(String url) {
        OkHttpClient client=new OkHttpClient();
        Request request=new Request.Builder().url(url).build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("client","请求失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.e("Main","request success");
                Message message=new Message();
                message.what=2001;
                message.obj=response.body().string();
                handler.sendMessage(message);
            }
        });
    }
}
